#!/bin/bash
CURRENT_DIR=`pwd`

APP_NAME=${CURRENT_DIR}/kinderCheckin
DEVICE_IP="192.168.2.253"
DEVICE_PORT=8000
USER_NAME="admin"
USER_PASSWD="litao888"

usage() {
    echo "开始运行:  sh alarmUpload.sh start"
    echo "停止运行:  sh alarmUpload.sh stop"
    echo "查询状态:  sh alarmUpload.sh status"
    echo "帮助   :  sh alarmUpload.sh help"
    exit 0
}

is_running(){
    pid=`ps -ef | grep ${APP_NAME} | grep -v grep|awk '{print $2}' `
    if [ -z "${pid}" ]; then
        return 1
    else
        return 0
    fi
}

startApp(){
    is_running
    if [ $? -eq "0" ]; then
        echo "${APP_NAME} is already running. pid=${pid}"
    else
       # nohup ${APP_NAME} ${DEVICE_IP} ${DEVICE_PORT} ${USER_NAME} ${USER_PASSWD} > /var/log/alarmUpload.log 2>&1 &
       ${APP_NAME} ${DEVICE_IP} ${DEVICE_PORT} ${USER_NAME} ${USER_PASSWD}
      #  sleep 5
      #  is_running
      #  if [ $? -eq "0" ]; then
      #      echo "${APP_NAME} is running, pid = ${pid}"
      #  else
      #      echo "${APP_NAME} start failed, check /var/log/alarmUpload.log for more information!"
      #  fi
    fi
}

stopApp(){
    is_running
    if [ $? -eq "0" ]; then
        kill -15 ${pid}
        sleep 3
        is_running
        if [ $? -eq "0" ]; then
            kill -9 ${pid}
        fi
    echo "APP ${APP_NAME} has stopped"
    else
        echo "${APP_NAME} is not running"
    fi
}

statusApp(){
    is_running
    if [ $? -eq "0" ]; then
    echo "${APP_NAME} is running. pid is ${pid}"
    else
    echo "${APP_NAME} is not running"
    fi
}

case "$1" in
    "start")
        startApp
    ;;
    "stop")
    stopApp
    ;;
    "status")
    statusApp
    ;;
    "help")
    usage
    ;;
    *)
    usage
    ;;
esac

exit 0

